import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {     // 全局管理的数据存储
    token: localStorage.getItem('token') ? localStorage.getItem('token') : '',   // token
    nick_name: '',
    username:''
};
export default new Vuex.Store({
    state,
    getters: {    // 监听数据变化的
        getStorage(state) {   // 获取本地存储的登录信息
            if (!state.token) {
                state.token = JSON.parse(localStorage.getItem(key))
            }
            return state.token
        }
    },
    mutations: {
        setToken(state, value) { // 设置存储token
            state.token = value;
            localStorage.setItem('token', value);
        },
        removeToken() {  // 删除token
            localStorage.removeItem('token');
        },
        setUserInfo(state, value) {
            state.nick_name = value.nickname;
            state.username = value.username;
            localStorage.setItem('nickname', value.nickname);
            localStorage.setItem('username', value.username);
        },
        removeUserInfo() {
            localStorage.removeItem('username')
            localStorage.removeItem('nickname')
        }
    }
})
